package xiangmu

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
	"zhuandan/data_source"
	"zhuandan/models"
)

type MubiaoBody struct {
	ID          int64  `json:"id" gorm:"id"`
	Xiangmuid   int64  `json:"xiangmuid" gorm:"xiangmuid"`
	Addtime     int64  `json:"addtime" gorm:"addtime"`
	Mubiao      string `json:"mubiao" gorm:"mubiao"`
	Zerenrenid  int64  `json:"zerenrenid" gorm:"zerenrenid"`
	Zerenren    string `json:"zerenren" gorm:"zerenren"`
	Wctime      int64  `json:"wctime" gorm:"wctime"`
	Status      int64  `json:"status" gorm:"status"` // 1正常 2异常
	Jindu       int64  `json:"jindu" gorm:"jindu"`
	Jinzhan     string `json:"jinzhan" gorm:"jinzhan"`
	Updatetime  int64  `json:"updatetime" gorm:"updatetime"`
	Xiangmumbid int64  `json:"xiangmumbid" gorm:"xiangmumbid"`
	Adminid     int64  `json:"adminid" gorm:"adminid"`
	Zrrid       int64  `json:"zrid" gorm:"zrrid"`
	Gzzrrid     int64  `json:"gzzrrid" gorm:"gzzrrid"`
}

func Mubiaos(ctx *gin.Context) {
	request := MubiaoBody{}
	ctx.ShouldBind(&request)
	xiangmuid := request.Xiangmuid
	zerenrenid := request.Zerenrenid
	wctime := request.Wctime

	var mubiaos []models.Xiangmumb
	res := data_source.Db.Where("xiangmuid = ?", xiangmuid)
	if zerenrenid > 0 {
		res = res.Where("zerenrenid = ?", zerenrenid)
	}
	if wctime > 0 {
		res = res.Where("wctime >= ? and wctime < ?", wctime, wctime+86400)
	}
	res = res.Find(&mubiaos)
	var kridarr []int64
	for _, v := range mubiaos {
		kridarr = append(kridarr, v.ID)
	}
	var krs []models.Xiangmukr
	data_source.Db.Where("xiangmumbid in (?)", kridarr).Find(&krs)
	for k, m := range mubiaos {
		var krlist []models.Xiangmukr
		for _, r := range krs {
			if m.ID == r.Xiangmumbid {
				krlist = append(krlist, r)
			}
		}
		mubiaos[k].Kr = krlist
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    mubiaos,
		"msg":     "ok",
	})
	return

}

func Mubiaokradd(ctx *gin.Context) {
	request := MubiaoBody{}
	ctx.ShouldBind(&request)
	xiangmumbid := request.Xiangmumbid
	mubiao := request.Mubiao
	zerenrenid := request.Zerenrenid
	zerenren := request.Zerenren
	wctime := request.Wctime
	mb := models.Xiangmukr{
		Xiangmumbid: xiangmumbid,
		Addtime:     time.Now().Unix(),
		Mubiao:      mubiao,
		Zerenren:    zerenren,
		Zerenrenid:  zerenrenid,
		Wctime:      wctime,
		Status:      1,
	}
	data_source.Db.Create(&mb)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Mubiaokrstatus(ctx *gin.Context) {
	request := MubiaoBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	status := request.Status
	mb := models.Xiangmukr{
		Status: status,
	}
	data_source.Db.Where("id = ?", id).Updates(&mb)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
func Mubiaokrupdate(ctx *gin.Context) {
	request := MubiaoBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	mubiao := request.Mubiao
	zerenrenid := request.Zerenrenid
	zerenren := request.Zerenren
	wctime := request.Wctime
	mb := models.Xiangmukr{
		Mubiao:     mubiao,
		Zerenren:   zerenren,
		Zerenrenid: zerenrenid,
		Wctime:     wctime,
	}
	data_source.Db.Where("id = ?", id).Updates(&mb)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Mubiaoadd(ctx *gin.Context) {
	request := MubiaoBody{}
	ctx.ShouldBind(&request)
	xiangmuid := request.Xiangmuid
	mubiao := request.Mubiao
	zerenrenid := request.Zerenrenid
	zerenren := request.Zerenren
	wctime := request.Wctime
	mb := models.Xiangmumb{
		Xiangmuid:  xiangmuid,
		Addtime:    time.Now().Unix(),
		Mubiao:     mubiao,
		Zerenren:   zerenren,
		Zerenrenid: zerenrenid,
		Wctime:     wctime,
		Status:     1,
	}
	data_source.Db.Create(&mb)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Mubiaodel(ctx *gin.Context) {
	request := MubiaoBody{}
	ctx.ShouldBind(&request)
	adminid := request.Adminid
	id := request.ID
	if adminid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请传递操作人id",
		})
		return
	}

	var mubiao models.Xiangmumb
	data_source.Db.Where("id = ?", id).First(&mubiao)

	data_source.Db.Where("id = ?", id).Delete(&models.Xiangmumb{})

	adminlog := models.Adminlog{
		Adminid: adminid,
		Addtime: time.Now().Unix(),
		Content: "删除目标" + mubiao.Mubiao,
	}
	data_source.Db.Create(&adminlog)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Krdel(ctx *gin.Context) {
	request := MubiaoBody{}
	ctx.ShouldBind(&request)
	adminid := request.Adminid
	id := request.ID
	if adminid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请传递操作人id",
		})
		return
	}

	var mubiao models.Xiangmukr
	data_source.Db.Where("id = ?", id).First(&mubiao)

	data_source.Db.Where("id = ?", id).Delete(&models.Xiangmukr{})

	adminlog := models.Adminlog{
		Adminid: adminid,
		Addtime: time.Now().Unix(),
		Content: "删除kr" + mubiao.Mubiao,
	}
	data_source.Db.Create(&adminlog)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Mubiaoupdate(ctx *gin.Context) {
	request := MubiaoBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	mubiao := request.Mubiao
	zerenrenid := request.Zerenrenid
	zerenren := request.Zerenren
	wctime := request.Wctime
	mb := models.Xiangmumb{
		Mubiao:     mubiao,
		Zerenren:   zerenren,
		Zerenrenid: zerenrenid,
		Wctime:     wctime,
	}
	data_source.Db.Where("id = ?", id).Updates(&mb)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Mubiaostatus(ctx *gin.Context) {
	request := MubiaoBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	status := request.Status
	mb := models.Xiangmumb{
		Status: status,
	}
	data_source.Db.Where("id = ?", id).Updates(&mb)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
